package com.example.everying.service.system.impl;/*
 * 项目名称: everything
 * 文件名: SysVersionControlServiceImpl
 * 作者: 张泽阳
 * 日期: 2025/7/19
 * 描述:
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.everying.context.UserContext;
import com.example.everying.mapper.system.SysVersionControlMapper;
import com.example.everying.model.dto.system.CheckVersionDto;
import com.example.everying.model.dto.system.VersionPageDto;
import com.example.everying.model.entity.system.SysVersionControl;
import com.example.everying.model.vo.system.UpdateVo;
import com.example.everying.service.system.SysVersionControlService;
import com.example.everying.utils.Result;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class SysVersionControlServiceImpl extends ServiceImpl<SysVersionControlMapper, SysVersionControl> implements SysVersionControlService {
    @Override
    public Result<?> add(SysVersionControl sysVersionControl) {
        sysVersionControl.setUploadUser(UserContext.getUser().getId());
        sysVersionControl.setCreateTime(LocalDateTime.now());
        sysVersionControl.setUpdateTime(LocalDateTime.now());
        sysVersionControl.setCreateTime(LocalDateTime.now());
        sysVersionControl.setUpdateTime(LocalDateTime.now());
        int insert = baseMapper.insert(sysVersionControl);
        if (insert > 0){
            return Result.ok(baseMapper.selectById(sysVersionControl.getId()));
        }
        return Result.fail("添加失败");
    }

    @Override
    public Result<?> getList(VersionPageDto versionPageDto) {
        QueryWrapper<SysVersionControl> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        Page<SysVersionControl> pageParam = new Page<>(versionPageDto.getPage(), versionPageDto.getLimit());
        Page<SysVersionControl> sysVersionControls = baseMapper.selectPage(pageParam, queryWrapper);
        return Result.ok(sysVersionControls);
    }

    @Override
    public Result<?> checkVersion(CheckVersionDto checkVersionDto) {
        SysVersionControl latestVersion = baseMapper.getLatestVersion();


        UpdateVo updateVo = new UpdateVo();
        if (latestVersion == null || (latestVersion.getAppVersion().equals(checkVersionDto.getAppVersion()) && latestVersion.getAppVersionName().equals(checkVersionDto.getAppVersionName()))){
            updateVo.setUpdateStatus("4");
            return Result.ok(updateVo);
        }
        updateVo.setApkDownloadPath(latestVersion.getApkDownloadPath());
        updateVo.setWgtDownloadPath(latestVersion.getWgtDownloadPath());

//        if(!(latestVersion.getAppVersion().equals(checkVersionDto.getAppVersion())) && !(latestVersion.getAppVersionName().equals(checkVersionDto.getAppVersionName()))){
//            updateVo.setUpdateStatus("3");
//            return Result.ok(updateVo);
//        }

        // 应用版本名称不一致更新apk
        if( !(latestVersion.getAppVersionName().equals(checkVersionDto.getAppVersionName()))){
            updateVo.setUpdateStatus("1");
            return Result.ok(updateVo);
        }

        // 应用版本名称不一致更新apk
        if(!(latestVersion.getAppVersion().equals(checkVersionDto.getAppVersion())) && latestVersion.getAppVersionName().equals(checkVersionDto.getAppVersionName())){
            updateVo.setUpdateStatus("2");
            return Result.ok(updateVo);
        }
        return null;
    }
}
